from PyPDF2 import PdfFileReader, PdfFileWriter

def split_pdf_1(infn, startpage, endpage):
    """
    infn: 切分的pdf
    startpage: 从startpage页开始切分，默认从第一页开始切分
    endpage：直到endpage页切分结束
    """
    pdf_output = PdfFileWriter()
    pdf_input = PdfFileReader(open(infn, 'rb'))
    page_count = pdf_input.getNumPages()

    # endpage必须大于startpage，endpage必须小于总页数
    if startpage > endpage:
        print("startpage > endpage")
        return
    print(page_count)
    if endpage > page_count:
        print("endpage > page_count")
        return
    # 读取对应页进行保存
    for i in range(startpage, endpage):
        pdf_input.getPage(i)
        pdf_output.addPage(pdf_input.getPage(i))

    pdf_output.write(open("e:\\1_result.pdf", "wb"))

split_pdf_1("e:\\t.pdf",25,35)
